package com.example.lesson_10_25.controller;

import com.example.lesson_10_25.CommonResult;
import com.example.lesson_10_25.pojo.User;
import com.example.lesson_10_25.pojo.User_Role;
import com.example.lesson_10_25.pojo.VueUser;
import com.example.lesson_10_25.service.RoleService;
import com.example.lesson_10_25.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @PACKAGE_NAME: com.example.lesson_10_25.controller
 * @NAME: RoleController
 * @USER: jiang000
 * @DATE: 2023/10/26
 **/
@RestController
public class RoleController {
    @Autowired
    private RoleService roleService;
    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }
    // 获取所有的权限列表
    @GetMapping("/role/getRoles")
    public CommonResult<?> getRoles() {
        return CommonResult.success(roleService.getRoles());
    }

    @RequestMapping(value = "/role/update/{rolename}", method = RequestMethod.PUT)
    public CommonResult<?> updateRole(@PathVariable String rolename, @RequestBody User_Role role){
        roleService.deleteRole(role.getName());
        roleService.updateDescription(role.getName(), role.getDescription());
        int len = role.getPermissionList().size();
        for(int i = 0; i < len; ++i){
            roleService.addRole(role.getName(), role.getPermissionList().get(i));
        }
        return CommonResult.success("更新成功");
    }
    @RequestMapping(value = "/role/add/{rolename}", method = RequestMethod.PUT)
    public CommonResult<?> addRole(@PathVariable String rolename, @RequestBody User_Role role){
        roleService.addnewRole(role.getName(), role.getDescription());
        return updateRole(rolename,role);
    }



}
